# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.industryDao import IndustryDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertIndustry', methods=['POST'])
def insert_industryNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'indusName': request.json.get('indusName', ''),
        'indusPcode': request.json.get('indusPcode', ''),
        'indusCode': request.json.get('indusCode', ''),
        'indusIsLock': request.json.get('indusIsLock', 0),
        'indusSort': request.json.get('indusSort', 99)
    }

    info2 = IndustryDao().insertIndus(info)
    if info2:
        return jsonify({"message": {"code": 1, "status": "success"},"addInfo":info})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findIndustryById', methods=['POST'])
def find_industry_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    indusId = request.json.get('indusId', '')
    info = IndustryDao().findIndusById(indusId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findIndustryByCondition', methods=['POST'])
def find_industry_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传�?pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = IndustryDao().findIndusByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                     multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteIndustryByIds', methods=['POST'])
def delete_industry_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传�?
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = IndustryDao().deleteIndus(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateIndustryById', methods=['POST'])
def update_industry_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        indusId = request.json['indusId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = IndustryDao().selectIndus(indusId)
    info = {
        'indusId': request.json['indusId'],
        'indusName': request.json.get('indusName', oldInfo[1]),
        'indusPcode': request.json.get('indusPcode', oldInfo[2]),
        'indusCode': request.json.get('indusCode', oldInfo[3]),
        'indusIsLock': request.json.get('indusIsLock', oldInfo[4]),
        'indusSort': request.json.get('indusSort', oldInfo[5])
    }

    info = IndustryDao().updateIndus(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
